package myta.bean;

import java.util.Map;

import javax.faces.component.html.HtmlInputText;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;

public class SemesterData extends FormDataBase{
	private HtmlInputText txtidsemester;
	private HtmlInputText txtsemester;
	private HtmlInputText txttahunajaran;
	
	@Override
	public void init() throws Exception {
		formName = "semester";
		
		txtidsemester = new HtmlInputText();
		txtsemester = new HtmlInputText();
		txttahunajaran = new HtmlInputText();
	}

	@Override
	public void generateID() throws Exception {
		String sql = "call idsemester()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtidsemester.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idsemester")){
			String idsemester = map.get("idsemester");
			
			String sql = "select * from semester where idsemester = ? order by idsemester desc";
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idsemester);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtidsemester.setValue(datarow.getString("idsemester"));
			txtsemester.setValue(datarow.getString("semester"));
			txttahunajaran.setValue(datarow.getString("thnajaran"));
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO semester(idsemester,semester,thnajaran) VALUES(?,?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtidsemester.getValue().toString());
		command.setObject(2, Integer.valueOf(txtsemester.getValue().toString()));
		command.setObject(3, Integer.valueOf(txttahunajaran.getValue().toString()));
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE semester SET semester=?, thnajaran=? where idsemester = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, Integer.valueOf(txtsemester.getValue().toString()));
		command.setObject(2, Integer.valueOf(txttahunajaran.getValue().toString()));
		command.setObject(3, txtidsemester.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtidsemester() {
		return txtidsemester;
	}

	public void setTxtidsemester(HtmlInputText txtidsemester) {
		this.txtidsemester = txtidsemester;
	}

	public HtmlInputText getTxtsemester() {
		return txtsemester;
	}

	public void setTxtsemester(HtmlInputText txtsemester) {
		this.txtsemester = txtsemester;
	}

	public HtmlInputText getTxttahunajaran() {
		return txttahunajaran;
	}

	public void setTxttahunajaran(HtmlInputText txttahunajaran) {
		this.txttahunajaran = txttahunajaran;
	}
}
